window.onload=function() {

    let vm = new Vue({
        el: "#cart",
        data:{
            userId:JSON.parse(window.sessionStorage.getItem("user")).data.userId,
            cartData:{},
            totalPrice:"",
            orderGo:[]
        },
        methods: {
            showCart(){
                axios
                    .get("http://localhost:9090/findCart",{
                        params:{
                            userId:this.userId
                        }
                    })
                    .then((result)=>{
                        console.log(result);
                        this.cartData=result.data;
                        this.totalPrice=result.data.totalPrice;
                        console.log(this.totalPrice);
                    })
            },
            dec(cartshopping){
                console.log("11111")
                console.log(cartshopping);
                if(cartshopping.quantity<=1){
                    alert("数量不能少于0");
                    return false;
                }else{
                    cartshopping.quantity=cartshopping.quantity-1;
                    cartshopping.totalPrice=cartshopping.totalPrice-cartshopping.commoditySku.price;
                    this.totalPrice=this.totalPrice-cartshopping.commoditySku.price;
                }
                for(let i=0;i<this.cartData.cartShoppingList.length;i++){
                    if(this.cartData.cartShoppingList[i].id==cartshopping.id){
                        this.cartData.cartShoppingList[i]=cartshopping;
                        break;
                    }
                }
                axios
                    .put("http://localhost:9090/dec",cartshopping)
                    .then((result)=>{
                        console.log(result)
                    })
                    .catch((err)=>{
                        console.log(err);
                    });
            },
            inc(cartShopping){
                console.log("2222")
                console.log(cartShopping);
                if(cartShopping.quantity>=cartShopping.commoditySku.stock){
                    alert("已经超出库存");
                    return false;
                }else{
                    cartShopping.quantity=cartShopping.quantity+1;
                    cartShopping.totalPrice=cartShopping.totalPrice+cartShopping.commoditySku.price;
                    this.totalPrice=this.totalPrice+cartShopping.commoditySku.price;
                }
                for(let i=0;i<this.cartData.cartShoppingList.length;i++){
                    if(this.cartData.cartShoppingList[i].id==cartShopping.id){
                        this.cartData.cartShoppingList[i]=cartShopping;
                        break;
                    }
                }
                axios
                    .put("http://localhost:9090/inc",cartShopping)
                    .then((result)=>{
                        console.log(result)
                    })
                    .catch((err)=>{
                        console.log(err);
                    });
            },
            remove(cartShopping){
                confirm("确认要移除吗？");
                console.log(cartShopping.id);
                for(let i=0;i<this.cartData.cartShoppingList.length;i++){
                    if(this.cartData.cartShoppingList[i].id==cartShopping.id){
                        this.cartData.cartShoppingList.splice(i,1);
                        console.log(this.cartData.cartShoppingList)
                        break;
                    }
                }

                axios
                    .delete("http://localhost:9090/remove",{
                        params: {
                            cartShoppingId:cartShopping.id
                        }
                    })
                    .then((result)=>{
                        console.log(result);
                    })
                    .catch((err)=>{
                        console.log(err);
                    })
            },
            addRemove(cartShopping){
                console.log(document.getElementById(cartShopping.id).checked);
                if (document.getElementById(cartShopping.id).checked){
                    this.orderGo.push(cartShopping);
                }else{
                    for(let i =0;i<this.orderGo.length;i++){
                        if(this.orderGo[i]==cartShopping){
                            this.orderGo.splice(i,1);
                            break;
                        }
                    }
                }
                console.log(this.orderGo);
            },
            orderToAdd(){
                Document.cookie="orderGo="+JSON.stringify(this.orderGo);
                let coo = Document.cookie.trim();
                console.log(JSON.parse(Document.cookie.trim().split("=")[1]));
                window.location.href="checkout.html"
            }

        },
        created(){
            this.showCart();
        },

    });
}